# Author    :Dun_Hz
# Time      :2024/3/11 9:56
class Node:
    def __init__(self, name, type='dir'):
        self.name = name
        self.type = type
        self.children = []
        self.parent = None

    def __repr__(self):
        return self.name


n = Node('Hello')
n2 = Node('World')
n.children.append(n2)
n2.parent = n
print(n)

class FileSystemTree:
    def __init__(self):
        self.root = Node('/')
        self.now = self.root

    def makedir(self, name):
        # name以 / 结尾
        if name[-1] != '/':
            name += '/'
        node = Node(name)
        self.now.children.append(node)
        node.parent = self.now

    def ls(self, name):
        return self.now.children

    def cd(self, name):
        if name[-1] !='/':
            name+='/'
        if name == '../':
            self.now = self.now.parent
            return
        for child in self.now.children:
            if child.name == name:
                self.now = child
                return
        raise ValueError('invalid dir')

tree = FileSystemTree()
tree.makedir('var')
tree.makedir('bin')
tree.makedir('user')
print(tree.ls(tree.root))
tree.cd('bin')
tree.makedir('python')
print(tree.ls(tree.root))
tree.cd('../')
print(tree.ls(tree.root))
